package com.kgc.scd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.scd.bean.Comment;
import com.kgc.scd.bean.Product;
import com.kgc.scd.bean.ProductVO;
import com.kgc.scd.service.CommentService;
import com.kgc.scd.service.ProductService;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: WangKuoKuo
 * @create: 2022-06-11 09:52
 * @description:
 **/
@Slf4j
@RestController
@Api(tags = "商品详情入口")
// @RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CommentService commentService;

    @GetMapping("/list")
    @ApiOperation("根据商品id查询商品")
    @ApiImplicitParam(name = "proId", value = "商品id")
    public RequestResult<ProductVO> listProduct(@RequestParam String proId) {
        if (null == proId ) {
            return ResultBuildUtil.fail("501", "商品不存在！");
        }

        ProductVO product = productService.queryProductById(proId);
        int commentCount = productService.queryProductByIdCommentCount(product.getProId());
        product.setCommentCount(commentCount);
        int buyCount = productService.queryProductByIdBuyCount(proId);
        product.setBuyCount(buyCount);
        log.info("商品查询成功！商品详情：{}", product);

        return ResultBuildUtil.success(product);
    }

    @GetMapping("/commentPage")
    @ApiOperation("商品评论分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commProId", value = "评论商品id"),
    })
    public RequestResult<Page> listComment(@RequestParam String commProId,
                                           @RequestParam Integer pageNo,
                                           @RequestParam Integer pageSize,
                                           @RequestParam Integer orderBy,
                                           @RequestParam Integer commentLevel) {

        if (null == commProId ) {
            return ResultBuildUtil.fail("502","订单号不存在");
        }
        // 分页插件
        Page<Comment> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<Comment> queryWrapperByCommId = new QueryWrapper<>();
        queryWrapperByCommId.eq("comm_proid", commProId);
        // 评价升序
        if(orderBy == 0){
            queryWrapperByCommId.orderByAsc("comm_datetime");
        }
        // 评价降序
        if(orderBy == 1){
            queryWrapperByCommId.orderByDesc("comm_datetime");
        }
        // 只看好评
        if(commentLevel == 1)
            queryWrapperByCommId.eq("comm_level",1);
        // 只看中评
        if(commentLevel == 2)
            queryWrapperByCommId.eq("comm_level",2);
        // 只看差评
        if(commentLevel == 3)
            queryWrapperByCommId.eq("comm_level",3);

        log.info("商品查询成功！商品详情：{}", "分页展示商品" + pageInfo);
        commentService.page(pageInfo, queryWrapperByCommId);
        return ResultBuildUtil.success(pageInfo);
    }


}
